﻿using System;
using System.Collections;
using System.Data;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;

namespace AnyOffice.module.dept_select
{
    public partial class dept_list : BasePage
    {           
        private int count;
        private StringBuilder stringBuilder;
        private string strTemp;      
        public string strDeptList;
        public dept_list()
        {
            this.stringBuilder = new StringBuilder();
            this.strDeptList = "";
        }
        private void BindTree(ref StringBuilder strDept, int DeptId, string strTemp)
        {
            DataTable table = new DepartmentDao().SelectChildAll(base.Conn, DeptId);
            if (table.Rows.Count > 0)
            {
                strTemp = strTemp + "&nbsp;&nbsp;&nbsp;";
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    string strDeptList = this.strDeptList;
                    this.strDeptList = strDeptList + "deptlist[" + this.count.ToString() + "]=\"" + table.Rows[i]["DeptId"].ToString() + "\";\n";
                    this.count++;
                    strDept.Append("<tr class=\"TableControl\">\n");
                    strDept.Append("<td class=\"menulines\" id=\"DEPT_" + table.Rows[i]["DeptId"].ToString() + "\" name=\"" + table.Rows[i]["DeptName"].ToString() + "\" onclick=\"javascript:click_dept('" + table.Rows[i]["DeptId"].ToString() + "')\">" + strTemp + "├" + table.Rows[i]["DeptName"].ToString() + "</td>\n");
                    strDept.Append("</tr>\n");
                    this.BindTree(ref strDept, int.Parse(table.Rows[i]["DeptId"].ToString()), strTemp);
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            DataTable table;
            this.ViewState["ToId"] = (Request.QueryString["ToId"] != null) ? Request.QueryString["ToId"].ToString() : "ToId";
            this.ViewState["ToName"] = (Request.QueryString["ToName"] != null) ? Request.QueryString["ToName"].ToString() : "ToName";
            int deptId = (Request.QueryString["dept_id"] == null) ? 0 : int.Parse(Request.QueryString["dept_id"].ToString());
            this.ViewState["DeptId"] = deptId;
            string str = (Request.QueryString["no_whole_dept"] != null) ? Request.QueryString["no_whole_dept"].ToString() : "";
            if (str != string.Empty)
            {
                this.pnlAllDept.Visible = false;
            }
            else
            {
                this.pnlAllDept.Visible = true;
            }
            DepartmentDao dao = new DepartmentDao();
            if (deptId != 0)
            {
                table = dao.SelectByDeptId(base.Conn, deptId);
            }
            else
            {
                table = dao.SelectParentAll(base.Conn);
            }
            if (table.Rows.Count > 0)
            {
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    string strDeptList = this.strDeptList;
                    this.strDeptList = strDeptList + "deptlist[" + this.count.ToString() + "]=\"" + table.Rows[i]["DeptId"].ToString() + "\";\n";
                    this.count++;
                    this.stringBuilder.Append("<tr class=\"TableControl\">\n");
                    this.stringBuilder.Append("<td class=\"menulines\" id=\"DEPT_" + table.Rows[i]["DeptId"].ToString() + "\" name=\"" + table.Rows[i]["DeptName"].ToString() + "\" onclick=\"javascript:click_dept('" + table.Rows[i]["DeptId"].ToString() + "')\">├" + table.Rows[i]["DeptName"].ToString() + "</td>\n");
                    this.stringBuilder.Append("</tr>\n");
                    this.strTemp = "";
                    this.BindTree(ref this.stringBuilder, int.Parse(table.Rows[i]["DeptId"].ToString()), this.strTemp);
                }
            }
            this.litDept.Text = this.stringBuilder.ToString();
        }

 

 

    }
}
